package tr.edu.metu.is.mega.server.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import tr.edu.metu.is.mega.common.dto.SubmissionRevisionDTO;
import tr.edu.metu.is.mega.server.dao.SubmissionRevisionDAO;
import tr.edu.metu.is.mega.server.entity.Activity;
import tr.edu.metu.is.mega.server.entity.Submission;
import tr.edu.metu.is.mega.server.entity.Submissionrevision;
import tr.edu.metu.is.mega.server.entity.Team;

/**
 * Class to manage submission revisions.
 */
public class SubmissionRevisionManager {

    private final SubmissionRevisionDAO dao;

    /**
     * Default constructor.
     */
    public SubmissionRevisionManager() {
        dao = new SubmissionRevisionDAO();
    }

    public List<SubmissionRevisionDTO> getRevisions(int activityid, int teamid) {
        Submissionrevision submissionrevision = new Submissionrevision();
        Submission submission = new Submission();
        Activity activity = new Activity();
        activity.setPkid(activityid);
        submission.setActivity(activity);
        Team team = new Team();
        team.setPkid(teamid);
        submission.setTeam(team);
        submissionrevision.setSubmission(submission);
        List<Submissionrevision> findByExample = dao.findByExample(submissionrevision, false);
        List<SubmissionRevisionDTO> list = new ArrayList<SubmissionRevisionDTO>(findByExample.size());
        for (Submissionrevision rev : findByExample) {
            list.add(convert(rev));
        }
        return list;
    }

    private SubmissionRevisionDTO convert(Submissionrevision from) {
        SubmissionRevisionDTO to = new SubmissionRevisionDTO();
        to.setPkid(from.getPkid());
        to.setFilePath(from.getFilepath());
        to.setSubmissionDate(new Date(from.getSubmissiondate().getTime()));
        to.setSubmittedby(from.getSubmittedby());
        to.setActivityid(from.getSubmission().getActivity().getPkid());
        to.setTeamid(from.getSubmission().getTeam().getPkid());
        return to;
    }

}
